using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Xml;
using System.Media;
using CueCatDecoder;

namespace CueCatConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("CueCatDecoder by Scott Hanselman @ http://www.hanselman.com");
            if (string.IsNullOrEmpty(Properties.Settings.Default.AWSAccessKeyId) || (string.IsNullOrEmpty(Properties.Settings.Default.AWSSecretKey)))
            {
                throw new ArgumentException("Be sure to put your Amazon Web Services Access Key and Secret Key in the application config file!");
            }
            while (true)
            {
                Console.WriteLine("Scan item with CueCat now or press Q then Enter to exit");
                string input = Console.ReadLine();
                if (input.ToUpperInvariant() == "Q") { return; }
                Product product = null;
                try
                {
                    product = Decoder.Decode(input);
                    Console.WriteLine("Type: {0}.", product.Type.ToString());
                    Console.WriteLine("Barcode: {0}.", product.BarCode.ToString());
                    Console.WriteLine("Product ID: {0}.", product.ID.ToString());
                    Console.Write("Checking Amazon - standby...\r");
                    SoundPlayer spWave;
                    //spWave = new SoundPlayer(@"catmeow.wav");
                    spWave = new SoundPlayer(Properties.Resources.catmeow);
                    spWave.Play();


                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error while decoding CueCat input: {0}.", ex.ToString());
                }

                if (product != null)
                {
                    AmazonBook book = AmazonBookPopulator.CreateAmazonProduct(product, Properties.Settings.Default.AWSAccessKeyId, Properties.Settings.Default.AWSSecretKey);
                    if (book != null)
                    {
                        Console.WriteLine(String.Format("FOUND: {0} by {1}",book.Title,book.Author));
                    }
                }


            }
        }
    }
}
